package com.zhubayi.daka.task;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zhubayi.daka.mapper.MyUserMapper;
import com.zhubayi.daka.pojo.DateTable;
import com.zhubayi.daka.pojo.MyUser;
import com.zhubayi.daka.utils.DakaUtil;
import com.zhubayi.daka.utils.MailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentMap;

/**
 * @author 祝八一
 */
@Component
public class DakaTask {
    @Autowired
    private MyUserMapper myUserMapper;
    @Autowired
    private MailUtil mailUtil;
    @Autowired
    private DakaUtil dakaUtil;
    /**
     * 10秒一次
     */
    //每天凌晨12.10分执行
    //0 10 0 * * ? （注意日期域为0不是24）
    //@Scheduled(cron = "0/10 * * * * ?")
    @Scheduled(cron = "0 0 13 * * ?")
    public void data(){
        //2021年2月9日登记已存在!
        List<MyUser> myUsers = myUserMapper.selectList(null);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年M月dd日");
        String format = dateFormat.format(new Date());
       // String str=format+"登记已存在!";
        if(myUsers.size()>0&&myUsers!=null){
            System.out.println("开始执行");
            for (MyUser myUser : myUsers) {
                    ConcurrentMap<String, Object> map = dakaUtil.loginAndDaka(myUser.getUsername(), myUser.getPassword());
                    String msg=(String)map.get("msg");
                    List<DateTable> table = (List<DateTable>)map.get("table");
                    String data = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                    myUser.setStatus(msg+data);
                    myUserMapper.update(myUser,new UpdateWrapper<MyUser>().eq("id",myUser.getId()));
                    if (myUser.getEmail()!=null){
                        mailUtil.send(myUser,table);
                }
                System.out.println("学号:"+myUser.getUsername()+"自动打卡消息:"+msg+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            }
            System.out.println("执行成功");
        }

    }

    public static void main(String[] args) {
        //new DakaTask().data();
      // String data = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
       // System.out.println(data);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年M月dd日");
        String format = dateFormat.format(new Date());
        System.out.println(format);
    }
}
